import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  Query,
  HttpException,
  HttpStatus,
  UseFilters
} from '@nestjs/common'
import { AppService } from './app.service'
import { HttpExceptionFilter } from './exception/http-exception.filter'

@Controller('app')
export class AppController {
  constructor(
    // 依赖注入
    private readonly appService: AppService
  ) {}
  // restful API(一种规范、风格)
  // Get: 获取数据
  // Post: 插入数据
  // Put: 更新数据
  // Delete: 删除数据
  @Get('data/:id/user/:subid')
  @UseFilters(new HttpExceptionFilter())
  getAllData(@Param() params): string {
    console.log(params)
    // 获取到的parmas参数都是sting类型的
    if (!params.subid || !Number.isInteger(Number(params.id))) {
      throw new HttpException(
        '参数必须是一个整数',
        HttpStatus.FORBIDDEN
      )
    }
    return this.appService.getAllData(params)
  }

  @Post('da*ta') //参数：路径
  addData(@Body() body) {
    console.log(body)
    return this.appService.addData(body)
  }

  @Put('data')
  putData(@Query() query, @Body() body) {
    console.log(query, body)
    return this.appService.putData(query, body)
  }

  @Delete('data/:id')
  deleteData(@Param() params, @Body() body) {
    console.log(params, body)
    return this.appService.deleteData(params, body)
  }
}
